package com.groupseven.happycampus.controller;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.builder.ExcelReaderSheetBuilder;
import com.alibaba.excel.write.builder.ExcelWriterBuilder;
import com.alibaba.excel.write.builder.ExcelWriterSheetBuilder;
import com.groupseven.happycampus.common.bo.CarBo;
import com.groupseven.happycampus.common.instener.WebCarListener;
import com.groupseven.happycampus.common.pojo.Car;
import com.groupseven.happycampus.common.pojo.Refund;
import com.groupseven.happycampus.common.result.LayuiPageVo;
import com.groupseven.happycampus.common.result.R;
import com.groupseven.happycampus.service.RefundService;
import org.apache.commons.io.FileUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/refund")
public class RefundController {
    @Autowired
    private RefundService refundService;

    @RequestMapping("/refundList")
    public LayuiPageVo<Refund> refundPageData(@RequestParam Map<String, Object> params) {
        System.out.println(params);
        return refundService.refundPageData(params);
    }


    @RequestMapping("deleteRefund")
    public R deleteRefund(@RequestParam Integer id) {
        int result = refundService.deleteRefund(id);
        if (result == 1) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @RequestMapping("deleteRefundsByIds")
    public R deleteRefundsByIds(@RequestParam String ids) {
        int result = refundService.deleteRefundsByIds(ids);
        if (result > 0) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @RequestMapping("addRefund")
    public R addRefund(@RequestParam Map<String, Object> params) {
        int result = refundService.addRefund(params);
        if (result == 1) {
            return R.ok();
        } else {
            return R.error();
        }
    }

    @RequestMapping("updateRefund")
    public R updateRefund(@RequestParam Map<String, Object> params) {
        System.out.println(params);
        int result = refundService.updateRefund(params);
        if (result == 1) {
            return R.ok();
        } else {
            return R.error();
        }
    }
    @Autowired
    private WebCarListener webCarListener;

    //下载excel 模板
    public void templateDownload(HttpServletRequest request, HttpServletResponse response) throws IOException {
        //下载excel 的 响应头
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        //读取服务器上的模板文件 写到浏览器
        //得到模板文件的路径
        String realPath = request.getServletContext().getRealPath("/template/xxx.xlsx");

        //根据文件路径 创建File 对象
        File file = new File(realPath);

        // 把File  对象转为 字节数组
        byte[] bytes = FileUtils.readFileToByteArray(file);
        // 下载的文件名 是中文  要进行转码  要不然会出现乱码
        String filename = URLEncoder.encode("订单", "utf-8");

        // 告诉浏览器  下载文件
        response.setHeader("Content-Disposition", "attachment;filename=" + filename + ".xlsx");

        //得到指向浏览器的输出流  向浏览器写输出
        ServletOutputStream outputStream = response.getOutputStream();
        // 把字节数组 输出到浏览器
        outputStream.write(bytes);

        outputStream.close();
    }
    @RequestMapping("/import")
    public R importExcel(MultipartFile file) throws IOException {
        // 避免遇见请求报错
        if (file != null) {
            ExcelReaderBuilder read = EasyExcel.read(file.getInputStream(), CarBo.class, webCarListener);
            ExcelReaderSheetBuilder sheet = read.sheet();
            sheet.doRead();
        }
        return R.ok();
    }
    //导出列表数据     把满足条件的数据 查询出来  写到excel 中  ，把excel 写到浏览器
    @RequestMapping("/export")
    public void export(Car car, HttpServletResponse response, Refund Refund) throws IOException {
        //下载excel 的 响应数据格式
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        // 下载的文件名 是中文  要进行转码  要不然会出现乱码
        String filename = URLEncoder.encode("订单信息导出", "utf-8");

        // 告诉浏览器  下载文件
        response.setHeader("Content-Disposition", "attachment;filename=" + filename + ".xlsx");

        //得到指向浏览器的输出流  向浏览器写输出
        ServletOutputStream outputStream = response.getOutputStream();

        ExcelWriterBuilder write = EasyExcel.write(outputStream, CarBo.class);

        ExcelWriterSheetBuilder sheet = write.sheet();
        //准备满足条件的集合数据
        List<CarBo> refunds = refundService.refundExcelData(Refund);
        // 把这个集合数据 写到 输出流
        sheet.doWrite(refunds);
    }

}
